(function(){
    // 点击注册时发送验证码逻辑
    var _tmpRegData = {};
    var _inMcodeRequest = false;

    var _mcoderegbtnenable = false;
    // 更新点击注册领红包按钮状态
    function _updateMcodeRegbtn() {
        var enable = !_inMcodeRequest &&
            $("#JS-regpanel .JS-input_mobile").val().trim() != "" &&
            $("#JS-regpanel .JS-input_pwd").val() != "" &&
            $("#JS-regpanel .JS-input_captcha").val().trim() != "";
        if (enable) {
            $("#JS-regpanel .JS-regbtn").removeClass("reg_finish_btn_dis");
        } else {
            $("#JS-regpanel .JS-regbtn").addClass("reg_finish_btn_dis");
        }
        _mcoderegbtnenable = enable;
    }
    $("#JS-regpanel .JS-input_mobile").bind("input", _updateMcodeRegbtn);
    $("#JS-regpanel .JS-input_pwd").bind("input", _updateMcodeRegbtn);
    $("#JS-regpanel .JS-input_captcha").bind("input", _updateMcodeRegbtn);

    $("#JS-regpanel .JS-regbtn").bind("click", function(){
        if (_inMcodeRequest == true || !_mcoderegbtnenable) return;
        var mobile = $("#JS-regpanel .JS-input_mobile").val().trim();
        var password = $("#JS-regpanel .JS-input_pwd").val();
        var captcha = $("#JS-regpanel .JS-input_captcha").val().trim();
        if (!P2PWAP.util.checkMobile(mobile)) {
            P2PWAP.ui.showErrorTip('手机号格式不正确');
            return;
        }
        if (!P2PWAP.util.checkPassword(password)) {
            P2PWAP.ui.showErrorTip('密码格式不正确，请输入6-16个字符');
            return;
        }
        if (!P2PWAP.util.checkCaptcha(captcha)) {
            P2PWAP.ui.showErrorTip('图形验证码不正确');
            return;
        }

        _tmpRegData['mobile'] = mobile;
        _tmpRegData['password'] = password;
        _tmpRegData['checkcode'] = captcha;
        _tmpRegData['type'] = 1;
        if ($("#token_id").length > 0) {
            _tmpRegData['token_id'] = $("#token_id").val();
            _tmpRegData['token'] = $("#token").val();
        } else {
            _tmpRegData['active'] = 1;
        }
        _inMcodeRequest = true;
        $("#JS-regpanel input").attr("readonly", "true");
        _updateMcodeRegbtn();
        P2PWAP.util.ajax('/sendRegValidSMS.htm', 'post', function(json) {
            _inMcodeRequest = false;
            _updateMcodeRegbtn();
            $("#JS-regpanel input").removeAttr("readonly");
            if (json.code == "0000") {
                _showVerifyMobileDialog(json.returnValue);
            } else {
                _tmpRegData = {};
                P2PWAP.ui.showErrorTip(json.message);
            }
        }, function(msg) {
            _tmpRegData = {};
            _inMcodeRequest = false;
            _updateMcodeRegbtn();
            $("#JS-regpanel input").removeAttr("readonly");
            P2PWAP.ui.showErrorTip(msg);
        }, _tmpRegData);
    });

    // 验证手机框逻辑
    var _inRegRequest = false;
    var _reMcodeAjax = null;
    var _reMcodeTimer = null;
    var _finishRegbtnenable = false;
    function _updateFinishRegBtn() {
        var enable = !_inRegRequest && $("#JS-regverifypanel .JS-input_vcode").val().trim() != "";
        if (enable) {
            $("#JS-regverifypanel .JS-regbtn").removeClass("reg_finish_btn_dis");
        } else {
            $("#JS-regverifypanel .JS-regbtn").addClass("reg_finish_btn_dis");
        }
        _finishRegbtnenable = enable;
    }
    $("#JS-regverifypanel .JS-input_vcode").bind("input", _updateFinishRegBtn);

    // 重新获取验证码逻辑
    var _reMcodeBtn = $("#JS-regverifypanel .JS-mcodebtn");
    function _cleanMcodeBtn() {
        if (_reMcodeTimer == null) return;
        clearInterval(_reMcodeTimer);
        _reMcodeTimer = null;
        _reMcodeBtn.removeClass('dis_reset').text('重新发送');
    }
    function _updateMcodeBtn(timeRemained) {
        //var timeRemained = 60;
        _reMcodeBtn.addClass('dis_reset').text(timeRemained + '秒后可重发');
        _reMcodeTimer = setInterval(function() {
            timeRemained--;
            if (timeRemained < 1) {
                _cleanMcodeBtn();
            } else {
                _reMcodeBtn.text(timeRemained + '秒后可重发');
            }
        }, 1000);
    }
    _reMcodeBtn.bind("click", function() {
        if (_reMcodeAjax != null || _reMcodeTimer != null) return;
        _reMcodeBtn.addClass('dis_reset').text('正在发送');
        _reMcodeAjax = P2PWAP.util.ajax('/sendRegValidSMS.htm', 'post', function(obj) {
            if (obj.code == "0000"){
                _updateMcodeBtn(obj.returnValue);
            } else {
               P2PWAP.ui.showErrorTip(obj.message);
               _reMcodeBtn.removeClass('dis_reset').text('重新发送');
            }
        }, function(msg) {
            _reMcodeAjax = null;
            P2PWAP.ui.showErrorTip(msg);
            _reMcodeBtn.removeClass('dis_reset').text('重新发送');
        }, _tmpRegData); 
    });
    function _showVerifyMobileDialog(timeRemained) {
        $("#JS-regverifypanel .JS-mobilelabel").text("已向" + _tmpRegData['mobile'] + "发送短信验证码");
        $("#JS-regverifypanel .JS-input_vcode").val("");
        _updateMcodeBtn(timeRemained);
        P2PWAP.ui.addModalView($("#JS-regverifypanel")[0]);
        $("#JS-regverifypanel").show();
    }
    $("#JS-regverifypanel .JS-closebtn").bind("click", function() {
        if (_inRegRequest) return;
        if (_reMcodeAjax) {
            _reMcodeAjax.abort();
            _reMcodeAjax = null;
        }
        _cleanMcodeBtn();
        P2PWAP.ui.removeModalView($("#JS-regverifypanel")[0]);
        $("#JS-regverifypanel").hide();
    });

    //注册逻辑
    $("#JS-regverifypanel .JS-regbtn").bind("click", function(){
        if (_inRegRequest || !_finishRegbtnenable) return;
        if (_reMcodeAjax != null) return;
        var vcode = $("#JS-regverifypanel .JS-input_vcode").val().trim();
        var recommendCode = $(".JS-input_invite").val();
        if (recommendCode!=null){
        	recommendCode = recommendCode.trim();
        }
        if (!P2PWAP.util.checkMcode(vcode)) {
            P2PWAP.ui.showErrorTip('请填写4位数字验证码');
            return;
        }
        _inRegRequest = true;
        _updateFinishRegBtn();

        var regdata = {};
        //添加活动附带参数
        if (window['_eventRegisterAddParams']) {
            regdata = window['_eventRegisterAddParams'];
        } else {
            regdata['cn'] = '';
        }
        regdata['mobile'] = _tmpRegData['mobile'];
        regdata['password'] = _tmpRegData['password'];
        regdata['checkcode'] = _tmpRegData['checkcode'];
        regdata['mobilecode'] = vcode;
        regdata['recommendCode'] = recommendCode;
        regdata['isAjax'] = 1;
        if ($(".JS-input_invite").length > 0) regdata['cn'] = $(".JS-input_invite").val().trim();
        if (typeof regdata['event_id'] == 'undefined') regdata['event_id'] = '';
        if (typeof regdata['event_data'] == 'undefined') regdata['event_data'] = '';

        P2PWAP.util.ajax('/register.htm', 'post', function(json) {
        	if(json.code=="0000" || json.code=="000"){
        		alert("注册成功");
        		window.location.href= "/user/login/";
        	}else{
        		_inRegRequest = false;
                _updateFinishRegBtn();
                P2PWAP.ui.showErrorTip(json.message);
        	}
        	
        }, function(msg) {
            _inRegRequest = false;
            _updateFinishRegBtn();
            P2PWAP.ui.showErrorTip(msg);
        }, regdata);
    });
})();
